#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

namespace nHydrate.Generator.Common.Util
{
	/// <summary>
	/// Summary description for AspStringBlock.
	/// </summary>
	public class AspStringBlock
	{
		private readonly string _inputString;
		private readonly bool _codeBlock;

		#region Constructors
		public AspStringBlock(string inputString, bool codeBlock)
		{
			_inputString = inputString;
			if(!_inputString.EndsWith(System.Environment.NewLine))
			{
				_inputString = _inputString +  System.Environment.NewLine;
			}
			_codeBlock = codeBlock;
		}
		#endregion

		public string GetOutput()
		{
			if(_codeBlock)
			{
				return _inputString;
			}
			else
			{
				return GetAspStringBlock();
			}
								
		}

		private string GetAspStringBlock()
		{
			var linesRegEx = new Regex("(.*)" + System.Environment.NewLine);
			var inLineAspRegEx = new Regex("<%=(.*?)%>");
			var sb = new StringBuilder();

			var lineMatches = linesRegEx.Matches(_inputString);
			foreach(Match lineMatch in lineMatches)
			{
				sb.Append("sb.AppendLine(");
				var line = lineMatch.Groups[1].Value;
				var inLineAspTags = inLineAspRegEx.Matches(line);
				var currentPosition = 0;
				foreach(Match inLineMatch in inLineAspTags)
				{
					var stringBlock = line.Substring(currentPosition, inLineMatch.Index - currentPosition);
					stringBlock = ReplaceQuotes(stringBlock);
					var codeBlock = inLineMatch.Groups[1].Value;
					sb.Append("\"" + stringBlock + "\"" + " + " + codeBlock + " + ");
					currentPosition = inLineMatch.Index + inLineMatch.Length;
				}
				var lastBlock = line.Substring(currentPosition, line.Length - currentPosition);
				lastBlock = ReplaceQuotes(lastBlock);
				sb.AppendLine("\"" + lastBlock + "\");");				
			}

			return sb.ToString();

		}

		private string ReplaceQuotes(string input)
		{
			input = input.Replace(@"\", @"\\");
			return input.Replace("\"", "\\\"");
		}

		public static string ParceAsp(string inAsp)
		{
			var outputStrings = new ArrayList();

			var codeSegmentRegex = new Regex(@"<%([^=][\s\S]*?[^\\]|[^=])%>");
			var codeSegments = codeSegmentRegex.Matches(inAsp);
			var currentPosition = 0;
			foreach (Match codeSegment in codeSegments)
			{
				var stringBlock = new AspStringBlock(inAsp.Substring(currentPosition, codeSegment.Index - currentPosition), false);
				var codeBlock = new AspStringBlock(codeSegment.Groups[1].Value, true);
				outputStrings.Add(stringBlock);
				outputStrings.Add(codeBlock);
				currentPosition = codeSegment.Index + codeSegment.Length;
			}
			var lastBlock = new AspStringBlock(inAsp.Substring(currentPosition, inAsp.Length - currentPosition), false);
			outputStrings.Add(lastBlock);

			var output = new StringBuilder();
			foreach (AspStringBlock outputStringBlock in outputStrings)
			{
				output.Append(outputStringBlock.GetOutput());
			}


			return output.ToString();
		}


	}
}

